#!/usr/bin/env python

import sys
import subprocess
from cStringIO import StringIO
from portconfig import get_port_config


def get_platform_hwsku():
    hwsku = None
    platform = None
    command = "show platform summary"
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    output = p.stdout.readlines()
    for line in output:
        tokens = line.split()
        if not tokens:
            continue
        if tokens[0].lower() == 'hwsku:':
            hwsku = tokens[1]
        elif tokens[0].lower() == 'platform:':
            platform = tokens[1]
    return (platform, hwsku)

def translate_line(line, ports):
    allowed_symbols = ['-', '_']
    sb = StringIO()
    start = 0
    end = 0
    while end < len(line):
        if line[end].isalnum() or line[end] in allowed_symbols:
            pass
        else: 
            # End of a word
            word = line[start:end]
            if word in ports:
                sb.write(ports[word]['alias'])
            else:
                sb.write(word)
            sb.write(line[end])
            start = end + 1
        end += 1
    if start != len(line):
        word = line[start:]
        if word in ports:
            sb.write(ports[word]['alias'])
        else:
            sb.write(word)
    return sb.getvalue()

def main():
    (platform, hwsku) = get_platform_hwsku()
    (ports, _) = get_port_config(hwsku, platform)
    for line in sys.stdin:
        sys.stdout.write(translate_line(line, ports))

if __name__ == "__main__":
    main()
